/*-----------------------------------------------
 * BookEZ - Booking System Project
 *-----------------------------------------------*/
package bookez.view.util;

import java.text.MessageFormat;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

/**
 * This is a utility class from Core JavaServer Faces 3rd.
 * It functions as a message factory: locate the message
 * by resourceId and return the the message as FacesMesage
 * @author Genzer
 *
 */
public class MessagesUtil {
	
	/**
	 * Return the message in default message bundle
	 * @param resourceId
	 * @param params
	 * @return
	 */
	public static FacesMessage getMessage( String resourceId, Object[] params ) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		String appBundle = app.getMessageBundle();
		
		return getMessage( appBundle, resourceId, params );		
	}//end method getMessage

	public static FacesMessage getMessage( String bundleName, String resourceId, Object[] params ) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		String appBundle = app.getMessageBundle();
		Locale locale = getLocale(context);
		ClassLoader loader = getClassLoader();
		
		String summary = getString(appBundle, bundleName, resourceId, locale, loader, params);
		
		if (summary == null)
			summary = "???" + resourceId + "???";
		
		String detail = getString(appBundle, bundleName,
				resourceId + "_detail", locale, loader, params);
		return new FacesMessage(summary, detail);
	}

	public static String getString(String bundle, String resourceId,
			Object[] params) {
		FacesContext context = FacesContext.getCurrentInstance();
		Application app = context.getApplication();
		String appBundle = app.getMessageBundle();
		Locale locale = getLocale(context);
		ClassLoader loader = getClassLoader();
		return getString(appBundle, bundle, resourceId, locale, loader, params);
	}

	/**
	 * 
	 * This method will scan default application resource bundle, and
	 * the secondary resource bundle (specified by client) and return
	 * the String which matches with resourceId.
	 * @param appBundle - application resource bundle
	 * @param secondBundle - user-specified resource bundle
	 * @param resourceId 
	 * @param locale
	 * @param loader
	 * @param params
	 * @return
	 */
	public static String getString(String appBundle, String secondBundle,
			String resourceId, Locale locale, ClassLoader loader,
			Object[] params) {
		String resource = null;
		ResourceBundle bundle;

		if (appBundle != null) {
			
			bundle = BundleUtil.getBundle( appBundle, locale, loader );
			if (bundle != null)
				try {
					resource = bundle.getString(resourceId);
				} catch (MissingResourceException ex) {
				}
		}

		if (resource == null) {
			bundle = BundleUtil.getBundle( secondBundle, locale, loader );
			if (bundle != null)
				try {
					resource = bundle.getString(resourceId);
				} catch (MissingResourceException ex) {
				}
		}

		if (resource == null)
			return null; // no match
		if (params == null)
			return resource;

		MessageFormat formatter = new MessageFormat(resource, locale);
		return formatter.format(params);
	}

	public static Locale getLocale(FacesContext context) {
		Locale locale = null;
		UIViewRoot viewRoot = context.getViewRoot();
		if (viewRoot != null)
			locale = viewRoot.getLocale();
		if (locale == null)
			locale = Locale.getDefault();
		return locale;
	}

	public static ClassLoader getClassLoader() {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		if (loader == null)
			loader = ClassLoader.getSystemClassLoader();
		return loader;
	}
	
}//end class MessageUtil
